package com.modules.service.sys.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.util.ConstantUtil;
import com.common.vo.Result;
import com.modules.dao.sys.DictDao;
import com.modules.dao.sys.DictSettingDao;
import com.modules.dto.sys.resp.DictEntityResp;
import com.modules.entity.sys.DictEntity;
import com.modules.entity.sys.DictSettingEntity;
import com.modules.service.sys.DictService;

import cn.hutool.core.bean.BeanUtil;

/**
 * service 字典
 * @author yyq
 */
@Service
public class DcitServiceImpl extends ServiceImpl<DictDao, DictEntity> implements DictService{


    @Autowired
    private DictSettingDao dictSettingDao;

    /**
     * 查询所有字典
     */
	@SuppressWarnings("unchecked")
	@Override
	public Result<?> getDictAll() {
		List<DictEntity> list = baseMapper.selectList(new LambdaQueryWrapper<DictEntity>());
		List<DictSettingEntity> setupList = dictSettingDao.selectList(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, ConstantUtil.USE).orderByAsc(DictSettingEntity::getSort));

		Map<String, Object> map = new HashMap<>();
		Map<String, Object> dictMap = new HashMap<>();
		List<DictEntityResp> resList = new ArrayList<>();
		for(DictEntity dict : list) {
			List<DictSettingEntity> tempList = new ArrayList<>();
			for(DictSettingEntity setup : setupList) {
				if(dict.getId().equals(setup.getDid())) {
					tempList.add(setup);
				}
			}
			dictMap.put(dict.getDictCode(), tempList);
			DictEntityResp resp = new DictEntityResp();
			BeanUtil.copyProperties(dict, resp);
			resp.setSetupList(tempList);
			resList.add(resp);
		}
		map.put("dict_map", dictMap);
		map.put("dict_list", resList);
		return Result.success(map);
	}

	/**
	 * 字典参数设置
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Result<?> selectSetting(Page<DictSettingEntity> page,Integer did) {
		LambdaQueryWrapper<DictSettingEntity> queryWrapper = new LambdaQueryWrapper<DictSettingEntity>();
		if(did != null) {
			queryWrapper.eq(DictSettingEntity::getDid, did);
		}
		queryWrapper.orderByAsc(DictSettingEntity::getSort);
		IPage<DictSettingEntity> iPage = dictSettingDao.selectPage(page, queryWrapper);
		return Result.success(iPage.getRecords(), iPage.getTotal());
	}

	@Override
	public Result<?> removeByIdSetting(String id) {
		int delete = dictSettingDao.deleteById(id);
		return delete>0?Result.success("删除成功"):Result.fail("删除失败");
	}

	@Override
	public Result<?> saveOrUpdateSetting(DictSettingEntity dict) {
		if(dict.getId() == null) {
			dictSettingDao.insert(dict);
		}else {
			dictSettingDao.updateById(dict);
		}
		return Result.success();
	}

	/**
	 * 获取字典text
	 */
	@Override
	public String getDictText(String code, String value) {
		DictEntity dict = baseMapper.selectOne(new LambdaQueryWrapper<DictEntity>().eq(DictEntity::getDictCode, code));
		DictSettingEntity dictSet = dictSettingDao.selectOne(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, ConstantUtil.USE)
				.eq(DictSettingEntity::getDid, dict.getId())
				.eq(DictSettingEntity::getCode, value));
		return dictSet!=null?dictSet.getRemarks():value;
	}

	/**
	 * 获取字典所有参数
	 */
	@Override
	public List<DictSettingEntity> getDictSetting(String code) {
		DictEntity dict = baseMapper.selectOne(new LambdaQueryWrapper<DictEntity>().eq(DictEntity::getDictCode, code));
		List<DictSettingEntity> list = dictSettingDao.selectList(new LambdaQueryWrapper<DictSettingEntity>()
				.eq(DictSettingEntity::getState, ConstantUtil.USE)
				.eq(DictSettingEntity::getDid, dict.getId()));
		return list;
	}

	/**
	 * 修改状态
	 * @return
	 */
	@Override
	public Result<?> updateSettingState(DictSettingEntity dict) {
		dictSettingDao.updateById(dict);
		return Result.success();
	}
	

	
	

}
